package com.zkx.mvp.persenter;

import android.text.TextUtils;

import com.zkx.mvp.api.HttpApi;
import com.zkx.mvp.contract.LoginContract;
import com.zkx.mvp.model.SessionResult;
import com.zkx.mvp.persenter.support.BaseSubscriberList;
import com.zkx.mvp.utils.LogUtils;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Author：zhangkx on 2017/6/16 16:55
 * Email：zkx9025@163.com
 * 登录Presenter的实现类
 */

public class LoginPresenter<T extends LoginContract.View> implements LoginContract.Presenter<T> {
    private T mView;
    private HttpApi httpApi;

    public LoginPresenter() {
        httpApi = HttpApi.getInstance();
    }

    @Override
    public void login(String userName, String passWord) {
        if (TextUtils.isEmpty(userName)) {
            mView.onNameError();
            return;
        }
        if (TextUtils.isEmpty(passWord)) {
            mView.onPassWordError();
            return;
        }
        httpApi.getSession()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriberList<List<SessionResult>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        LogUtils.i("LoginPresenter","e :" +e.toString());
                    }

                    @Override
                    public void onNext(List<SessionResult> list) {
                        if (list != null && list.size() > 0){
                            LogUtils.i("LoginPresenter","数据量："+list.size());
                            for (int i = 0; i < list.size(); i++) {
                                LogUtils.i("LoginPresenter","第"+i+"条数据："+list.get(i).toString());

                            }
                        }else{

                        }
                        mView.onSuccess(list.toString());
                        mView.complete();
                    }
                });
    }

    @Override
    public void attachView(T view) {
        mView = view;
    }

    @Override
    public void detachView() {
    }
}
